package com.hound;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

public class MyUnSafe {

    private MyUnSafe(){}

    public static Unsafe getInstance(){
        return Singletom.INSTANCE.getInstance();
    }

    private static enum Singletom{
        INSTANCE;
        private Unsafe unsafe;
        private Singletom(){
            try {
                // 反射获取 Unsafe 的成员变量 theUnsafe（2.2.10）
                Field field = Unsafe.class.getDeclaredField("theUnsafe");
                // 设置为可存取（2.2.11）
                field.setAccessible(true);
                // 获取该变量的值（2.2.12）
                this.unsafe = (Unsafe) field.get(null);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        public Unsafe getInstance(){
            return this.unsafe;
        }
    }
}
